
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../alert_business/video/recommend_video_res.dart';
import '../../hj_model/video_model.dart';
import '../../hj_utils/api_service/common_service.dart';
import '../../tools_base/debug_log.dart';
import '../../config/config.dart';
import 'package:tiktokadult/hj_model/splash/ads_model.dart';
import 'package:tiktokadult/hj_model/splash/domain_source_model.dart';
import 'package:tiktokadult/hj_page/pre_sale/limit_time_event/limit_time_event_provider.dart';
import 'package:tiktokadult/hj_page/pre_sale/pre_sale_provider.dart';
import 'package:tiktokadult/tools_base/global_store/local_ads_info_store.dart';
import 'package:tiktokadult/alert_business/splash/application_center.dart';
import 'package:tiktokadult/alert_business/splash/system_image_dialog.dart';
import 'package:tiktokadult/alert_business/splash/system_word_dialog.dart';


class ShortFollowLogic extends GetxController with GetSingleTickerProviderStateMixin {

  List<VideoModel>? dataArr;
  int currentPage = 1;
  RefreshController? refreshController;


  @override
  void onInit() {
    super.onInit();
    onInitData();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      startAdEvent();
    });
  }


  void onInitData() {
    dataArr = null;
    update();
    _loadData();
  }

  void onRefreshData() {
    _loadData();
  }

  void onLoadMoreData() {
    _loadData(page: currentPage + 1);
  }

  void _loadData({int page = 1, int size = 10}) async {
    try {
      RecommendListRes? retModel = await CommonService.fetchMyFollowDynamicList(page: page, size: size, type: 1);
      currentPage = page;
      dataArr ??= [];
      if (page == 1) {
        dataArr?.clear();
      }
      dataArr?.addAll(retModel?.vInfo ?? []);
      if (retModel?.hasNext == true) {
        refreshController?.loadComplete();
      } else {
        refreshController?.loadNoData();
      }
    } catch (e) {
      dataArr ??= [];
      refreshController?.loadComplete();
      debugLog(e);
    }
    refreshController?.refreshCompleted();
    update();
  }


  static bool showAdFinishAlert = false;
  startAdEvent() async {
    if (showAdFinishAlert) {
      return;
    }
    showAdFinishAlert = true;
    await _showSysAppDialog(); //系统提示
    await PreSaleProvider().showPreSaleAlert(); //预售弹窗
    await limitTimeProvider.handleLimitTimeEvent(); //获取限时活动
    await _showImageDialog(); //图片公告
    await _showWordDialog(); //文字公告
  }

  ///显示公告信息
  Future _showSysAppDialog() async {
    ///若已显示则不再弹出
    final appList = await getAdvByType(3); //9宫格应用广告
    if (appList.isNotEmpty) {
      await Get.dialog(ApplicationCenterDialog(
        appList: appList,
      ));
    }
  }

  ///显示图片公告
  Future _showImageDialog() async {
    List<AdsInfoModel> adsInfoList = await getAdvByType(2);
    for (int i = 0; i < adsInfoList.length; i++) {
      await Get.dialog(
        SystemImageDialog(
          model: adsInfoList[i],
        ),
      );
    }
  }

  //文字公告
  Future _showWordDialog() async {
    for (int i = 0; i < Config.wordAdsBeans.length; i++) {
      AnnounceInfoBean wordBean = Config.wordAdsBeans[i];
      if (wordBean.type == 0) {
        await Get.dialog(SystemWordDialog(
          content: wordBean.content.toString(),
          wordBean: wordBean,
        ));
      } else {
        await Get.dialog(
          SystemImageDialog(
            model: AdsInfoModel(
              cover: wordBean.cover,
              href: wordBean.href,
            ),
          ),
        );
      }
    }
  }


}
